package br.com.amadeus.android.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import br.com.amadeus.android.entities.Course;
import br.com.amadeus.android.entities.Forum;
import br.com.amadeus.android.entities.Message;
import br.com.amadeus.android.entities.Resource;
import br.com.amadeus.android.entities.SocialHistory;

public class Syncronizer {
		
	public static List<SyncronizerHelper> getActionsSyncronize(List<Course> listCourseLocal, List<Course> listCourseRemote){
		List<SyncronizerHelper> listHelper = new ArrayList<SyncronizerHelper>();
				
		//CURSOS
		List<Integer> listNewCourses = new ArrayList<Integer>();
		List<Integer> listRemoveCourses = new ArrayList<Integer>();
		
		List<Integer> listIdsRemote = new ArrayList<Integer>();
		List<Integer> listIdsLocal = new ArrayList<Integer>();
		
		for (Course course : listCourseRemote) {
			listIdsRemote.add(course.getId_amadeus());
		}
		
		for (Course course : listCourseLocal) {
			listIdsLocal.add(course.getId_amadeus());
		}
		
		//Verificando se houve inclusão de Cursos
		for (Integer id : listIdsRemote) {
			if (!listIdsLocal.contains(id)) {
				listNewCourses.add(id);
			}
		}
		
		//Verificando se houve exclusão de Cursos
		for (Integer id : listIdsLocal) {
			if (!listIdsRemote.contains(id)) {
				listRemoveCourses.add(id);
			}
		}
		
		//Listando os Cursos que foram inclusos
		List<Course> listCourses = new ArrayList<Course>();
		Collections.sort(listCourseRemote);
		for (Integer id : listNewCourses) {
						
			Course c = new Course();
			c.setId_amadeus(id);
			Course course = listCourseRemote.get(Collections.binarySearch(listCourseRemote, c));
			listCourses.add(course);
			
		}
		
		if (listNewCourses.size() > 0) {
			SyncronizerHelper shelp = new SyncronizerHelper(SyncronizerHelper.INSERT, SyncronizerHelper.COURSE, listCourses, null, null, null);
			listHelper.add(shelp);
		}
	
		
		//Listando os Cursos que foram excluídos
		List<Course> listCourses2 = new ArrayList<Course>();
		Collections.sort(listCourseLocal);
		for (Integer id : listRemoveCourses) {
			
			Course c = new Course();
			c.setId_amadeus(id);
			Course course = listCourseLocal.get(Collections.binarySearch(listCourseLocal, c));
			listCourses2.add(course);
			
		}
		
		if (listRemoveCourses.size() > 0) {
			SyncronizerHelper shelp2 = new SyncronizerHelper(SyncronizerHelper.DELETE, SyncronizerHelper.COURSE, listCourses2, null, null, null);
			listHelper.add(shelp2);
		}
				
		//RECURSOS
		List<Resource> listResourcesLocal = new ArrayList<Resource>();
		List<Resource> listResourcesRemote = new ArrayList<Resource>();
		
		List<Integer> listNewResourses = new ArrayList<Integer>();
		List<Integer> listRemoveResourses = new ArrayList<Integer>();
		
		listIdsRemote = new ArrayList<Integer>();
		listIdsLocal = new ArrayList<Integer>();
		
		for (Course course : listCourseLocal) {
			List<Resource> listR = course.getResourceList();
			for (Resource res : listR) {
				listResourcesLocal.add(res);
			}
		}
		
		for (Course course : listCourseRemote) {
			List<Resource> listR = course.getResourceList();
			for (Resource res : listR) {
				listResourcesRemote.add(res);
			}
		}
		
		for (Resource resource : listResourcesLocal) {
			listIdsLocal.add(resource.getAmadeus_id());
		}
		
		for (Resource resource : listResourcesRemote) {
			listIdsRemote.add(resource.getAmadeus_id());
		}
		
		//Verificando a inclusão de novos Recursos
		for (Integer id : listIdsRemote) {
			if (!listIdsLocal.contains(id)) {
				listNewResourses.add(id);
			}
		}
		
		//Verificando a exclusão de Recursos
		for (Integer id : listIdsLocal) {
			if (!listIdsRemote.contains(id)) {
				listRemoveResourses.add(id);
			}
		}
		
		//Listando os Recursos que foram inclusos
		List<Resource> listResources = new ArrayList<Resource>();
		Collections.sort(listResourcesRemote);
		for (Integer id : listNewResourses) {
			
			Resource r = new Resource();
			r.setAmadeus_id(id);
			Resource res = listResourcesRemote.get(Collections.binarySearch(listResourcesRemote, r));
			if (!listNewCourses.contains(res.getCourse().getId_amadeus())) {
				listResources.add(res);
			}
			
		}
		
		if (listNewResourses.size() > 0) {
			SyncronizerHelper shelp = new SyncronizerHelper(SyncronizerHelper.INSERT, SyncronizerHelper.RESOURCE, null, listResources, null, null);
			listHelper.add(shelp);
		}
		
		//Listando os Recursos que foram excluídos
		List<Resource> listResources2 = new ArrayList<Resource>();
		Collections.sort(listResourcesLocal);
		for (Integer id : listRemoveResourses) {
			
			Resource r = new Resource();
			r.setAmadeus_id(id);
			Resource res = listResourcesLocal.get(Collections.binarySearch(listResourcesLocal, r));
			listResources2.add(res);
		}
		
		if (listRemoveResourses.size() > 0) {
			SyncronizerHelper shelp = new SyncronizerHelper(SyncronizerHelper.DELETE, SyncronizerHelper.RESOURCE, null, listResources2, null, null);
			listHelper.add(shelp);
		}
				
		return listHelper;
	}
	
	public static List<SyncronizerHelper> getActionsSyncronizeTimeline(List<SocialHistory> localList, List<SocialHistory> remoteList){
		List<SyncronizerHelper> listHelper = new ArrayList<SyncronizerHelper>();
		
		if (remoteList.size() > localList.size()) {
			
			List<Integer> listNew = new ArrayList<Integer>();
			
			List<Integer> listIdsRemote = new ArrayList<Integer>();
			List<Integer> listIdsLocal = new ArrayList<Integer>();
			
			for (SocialHistory obj : localList) {
				listIdsLocal.add(obj.getAmadeusID());
			}
			
			for (SocialHistory obj : remoteList) {
				listIdsRemote.add(obj.getAmadeusID());
			}
			
			//Verificando de houve inclusão
			for (Integer obj : listIdsRemote) {
				if (!listIdsLocal.contains(obj)) {
					listNew.add(obj);
				}
			}
			
			List<SocialHistory> list = new ArrayList<SocialHistory>();
			Collections.sort(remoteList);
			for (Integer id : listNew) {
				
				SocialHistory sh = new SocialHistory(0);
				sh.setAmadeusID(id);
				SocialHistory obj = remoteList.get(Collections.binarySearch(remoteList, sh));
				list.add(obj);
			}
			
			SyncronizerHelper shelp = new SyncronizerHelper(0, null, null, null, list, null);
			listHelper.add(shelp);
		}
		
		return listHelper;
		
	}
			
	public static List<SyncronizerHelper> getActionsSyncronizeForum(ArrayList<Forum> listForumLocal, ArrayList<Forum> listForumRemote) {
		List<SyncronizerHelper> listHelper = new ArrayList<SyncronizerHelper>();
		ArrayList<Message> listMessage = new ArrayList<Message>();
		
		Collections.sort(listForumRemote);
		for (Forum forum : listForumLocal) {
			
			Forum f = new Forum();
			f.setAmadeus_id(forum.getAmadeus_id());
			Forum forumRemote = listForumRemote.get(Collections.binarySearch(listForumRemote, f));
			if (forum.getListMessages().size() < forumRemote.getListMessages().size()) {
				
				ArrayList<Integer> listIdLocal = new ArrayList<Integer>();
				ArrayList<Integer> listIdRemote = new ArrayList<Integer>();
								
				ArrayList<Message> listMsgLocal = forum.getListMessages();
				for (Message message : listMsgLocal) {
					listIdLocal.add(message.getAmadeus_id());
				}
				
				ArrayList<Message> listMsgRemote = forumRemote.getListMessages();
				for (Message message : listMsgRemote) {
					listIdRemote.add(message.getAmadeus_id());
				}
				
				Collections.sort(listMsgRemote);
				for (Integer amadeus_id : listIdRemote) {
					if (!listIdLocal.contains(amadeus_id)) {
						
						Message m = new Message();
						m.setAmadeus_id(amadeus_id);
						Message message = listMsgRemote.get(Collections.binarySearch(listMsgRemote, m));
						message.getForum().setId(forum.getId());
						listMessage.add(message);
						
					}
				}
					
			}
		}
		
		if (listMessage.size() > 0) {
			SyncronizerHelper help = new SyncronizerHelper(0, null, null, null, null, listMessage);
			listHelper.add(help);
		}
		
		return listHelper;
	}


}
